package td.game;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.widget.Toast;

public class Main extends Activity {
	//"Constants" that describes the size of the screen
	public static int SCREEN_WIDTH;
	public static int SCREEN_HEIGHT;
	public static final int KILL_APP = 0;
	public static final int DONT_KILL_APP = 1;
	
	private WifiManager wifi;
	private ConnectivityManager connManager;
	
	private ActivityManager activityManager;
	private static MemoryInfo memoryInfo;
	
	private MainGamePanel mainGamePanel;
	
	private static Typeface font;
	
	private static int IP;
	private static int subnetMask;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setTitle("Zoid Defense");
        
        wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    	connManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    	activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    	memoryInfo = new MemoryInfo();
    	activityManager.getMemoryInfo(memoryInfo);

		font = Typeface.createFromAsset(getAssets(), "fonts/disco.ttf");
        
        if (!wifi.isWifiEnabled()){
        	Toast.makeText(this, "Wifi is not enabled. Please connect to a wifi network and try again", Toast.LENGTH_LONG).show();
        	finish();
        } else {
	        
	        if(!connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()){
	        	Toast.makeText(this, "You are not connected to a wifi network. Connect then try again.", Toast.LENGTH_LONG).show();
	        	finish();        	
	        } else {
	        	IP = wifi.getConnectionInfo().getIpAddress();
	        	subnetMask = wifi.getDhcpInfo().netmask;
		        
		        SCREEN_WIDTH = getWindowManager().getDefaultDisplay().getWidth();
		        SCREEN_HEIGHT = getWindowManager().getDefaultDisplay().getHeight();
		        
		        mainGamePanel = new MainGamePanel(this, errorHandler);
		        setContentView(mainGamePanel);
	        }
        }
    }
    
    public final Handler errorHandler = new Handler(){
		@Override
		public void handleMessage(Message message){
			System.exit(0);
		}
    };
    
    public static Typeface getFont(){
    	return font;
    }

    @Override
    public void onBackPressed(){
    	System.exit(0);
    }
    
    public static void lala(){
    	System.exit(0);
    }
    
    public static String getIp(){
    	return Formatter.formatIpAddress(IP);
    }
    
    public static int getIntIp(){
    	return IP;
    }
    public static int getIntSubnetMask(){
    	return subnetMask;
    }
    
}